import 'dart:convert';

class UserModel {
  final int id;
  final String created;
  final int status;
  final String name;
  final String email;
  final String phone;
  final String username;
  final String? avatar;
  final String nickname;
  final List<RoleModel> roles;

  UserModel({
    required this.id,
    required this.created,
    required this.status,
    required this.name,
    required this.email,
    required this.phone,
    required this.username,
    this.avatar,
    required this.nickname,
    required this.roles,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    List<RoleModel> rolesList = [];
    if (json['roles'] != null) {
      rolesList = List.from(json['roles'])
          .map((role) => RoleModel.fromJson(role))
          .toList();
    }

    return UserModel(
      id: json['id'],
      created: json['created'],
      status: json['status'],
      name: json['name'],
      email: json['email'],
      phone: json['phone'],
      username: json['username'],
      avatar: json['avatar'],
      nickname: json['nickname'],
      roles: rolesList,
    );
  }
}

class RoleModel {
  final int id;
  final String name;
  final String? deleted;
  final String? created;
  final String? modified;
  final String? status;
  final String? remark;

  RoleModel({
    required this.id,
    required this.name,
    this.deleted,
    this.created,
    this.modified,
    this.status,
    this.remark,
  });

  factory RoleModel.fromJson(Map<String, dynamic> json) {
    return RoleModel(
      id: json['id'],
      name: json['name'],
      deleted: json['deleted'],
      created: json['created'],
      modified: json['modified'],
      status: json['status'],
      remark: json['remark'],
    );
  }
}